﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using SaleManagement.Entity;
using System.Data;

namespace SaleManagement.Repository
{
   public class ContactRepository:SaleManagement.Repository.Interfaces.IContactRepository
    {
       SaleSystemContext context = new SaleSystemContext();
        public IQueryable<Contact> All
        {
            get { return context.Contacts; }
        }

        public IQueryable<Entity.Contact> AllIncluding(params System.Linq.Expressions.Expression<Func<Entity.Contact, object>>[] includeProperties)
        {
            IQueryable<Contact> query = context.Contacts;
            foreach (var includeProperty in includeProperties)
            {
                query = query.Include(includeProperty);
            }
            return query;
        }

        public Entity.Contact Find(int id)
        {
            return context.Contacts.Find(id);
        }

        public void InsertOrUpdate(Entity.Contact entity)
        {
            if (entity.Id == default(int))
            {
                // New entity
                context.Contacts.Add(entity);
            }
            else
            {
                // Existing entity
                context.Entry(entity).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var entity = context.Contacts.Find(id);
            context.Contacts.Remove(entity);
        }

        public void Save()
        {
            context.SaveChanges();
        }

        public void Dispose()
        {
            context.Dispose();
        }
    }
}
